package cn.iocoder.yudao.module.ym.controller.admin.ticketlog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ym.controller.admin.ticketlog.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.ticketlog.TicketLogDO;
import cn.iocoder.yudao.module.ym.service.ticketlog.TicketLogService;

@Tag(name = "管理后台 - 工单日志")
@RestController
@RequestMapping("/ym/ticket-log")
@Validated
public class TicketLogController {

    @Resource
    private TicketLogService ticketLogService;

    @PostMapping("/create")
    @Operation(summary = "添加评论")
    public CommonResult<Long> createTicketLog(@Valid @RequestBody TicketLogSaveReqVO createReqVO) {
        return success(ticketLogService.createTicketLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单日志")
    public CommonResult<Boolean> updateTicketLog(@Valid @RequestBody TicketLogSaveReqVO updateReqVO) {
        ticketLogService.updateTicketLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单日志")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTicketLog(@RequestParam("id") Long id) {
        ticketLogService.deleteTicketLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TicketLogRespVO> getTicketLog(@RequestParam("id") Long id) {
        TicketLogDO ticketLog = ticketLogService.getTicketLog(id);
        return success(BeanUtils.toBean(ticketLog, TicketLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单日志分页")
    public CommonResult<PageResult<TicketLogRespVO>> getTicketLogPage(@Valid TicketLogPageReqVO pageReqVO) {
        PageResult<TicketLogDO> pageResult = ticketLogService.getTicketLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TicketLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单日志 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTicketLogExcel(@Valid TicketLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TicketLogDO> list = ticketLogService.getTicketLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工单日志.xls", "数据", TicketLogRespVO.class,
                        BeanUtils.toBean(list, TicketLogRespVO.class));
    }

}